package com.aihna.mall.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.aihna.common.idempotent.annotation.RepeatSubmit;
import com.aihna.common.log.annotation.Log;
import com.aihna.common.web.core.BaseController;
import com.aihna.common.mybatis.core.page.PageQuery;
import com.aihna.common.core.domain.R;
import com.aihna.common.core.validate.AddGroup;
import com.aihna.common.core.validate.EditGroup;
import com.aihna.common.log.enums.BusinessType;
import com.aihna.common.excel.utils.ExcelUtil;
import com.aihna.mall.domain.vo.StoreProductCateVo;
import com.aihna.mall.domain.bo.StoreProductCateBo;
import com.aihna.mall.service.IStoreProductCateService;

/**
 * 商品分类
 *
 * @author Joseph.Chen
 * @date 2024-03-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mall/storeProductCate")
public class StoreProductCateController extends BaseController {

    private final IStoreProductCateService storeProductCateService;

    /**
     * 查询商品分类列表
     */
    @SaCheckPermission("mall:storeProductCate:list")
    @GetMapping("/list")
    public R<List<StoreProductCateVo>> list(StoreProductCateBo bo) {
        List<StoreProductCateVo> list = storeProductCateService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出商品分类列表
     */
    @SaCheckPermission("mall:storeProductCate:export")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(StoreProductCateBo bo, HttpServletResponse response) {
        List<StoreProductCateVo> list = storeProductCateService.queryList(bo);
        ExcelUtil.exportExcel(list, "商品分类", StoreProductCateVo.class, response);
    }

    /**
     * 获取商品分类详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("mall:storeProductCate:query")
    @GetMapping("/{id}")
    public R<StoreProductCateVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(storeProductCateService.queryById(id));
    }

    /**
     * 新增商品分类
     */
    @SaCheckPermission("mall:storeProductCate:add")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody StoreProductCateBo bo) {
        return toAjax(storeProductCateService.insertByBo(bo));
    }

    /**
     * 修改商品分类
     */
    @SaCheckPermission("mall:storeProductCate:edit")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody StoreProductCateBo bo) {
        return toAjax(storeProductCateService.updateByBo(bo));
    }

    /**
     * 删除商品分类
     *
     * @param ids 主键串
     */
    @SaCheckPermission("mall:storeProductCate:remove")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(storeProductCateService.deleteWithValidByIds(List.of(ids), true));
    }


}
